package com.lc.pianostudent.model;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

import com.lc.pianostudent.activity.PlayVideoActivity;

import java.text.SimpleDateFormat;
import java.util.Date;

import VideoHandle.EpEditor;
import VideoHandle.EpVideo;
import VideoHandle.OnEditorListener;

/**
 * Created by Administrator on 2018/8/27.
 */

public class MyService extends Service{

    private Binder binder = new LocalBinder();
    private String videoUrl;
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("tag", "服务-------create");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("tag", "服务-------destory");
    }

    @Override
    public IBinder onBind(Intent arg0) {
        Log.i("tag", "服务-------onBind");
        return binder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i("tag", "服务-------onUnbind");
        return super.onUnbind(intent);
    }

    public class LocalBinder extends Binder {
        public MyService getService() {
            return MyService.this;
        }

        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply,
                                     int flags) throws RemoteException {
            //  参数 data是Activity中传输过来的数据。
            //  参数reply是Service反馈回去的数据。
            //  参数code和flags 在这里不用改动

            Bundle bundle = data.readBundle();
             videoUrl = bundle.getString("video"); //用于接收字符串
            Log.e("jyjyjyjy==",videoUrl+"====");
            execVideo();

            return super.onTransact(code, data, reply, flags);
        }

    }

    private void execVideo(){
        if(videoUrl != null && !"".equals(videoUrl)){

            EpVideo epVideo = new EpVideo(videoUrl);
            epVideo.clip(Float.parseFloat("1"),Float.parseFloat("2"));

            final String outPath = "now.mp4";
            EpEditor.exec(epVideo, new EpEditor.OutputOption(outPath), new OnEditorListener() {
                @Override
                public void onSuccess() {
//					Toast.makeText(EditActivity.this, "编辑完成:"+outPath, Toast.LENGTH_SHORT).show();

                    Log.e("jyjyjyjy==","成功===");
//                    Intent v = new Intent(Intent.ACTION_VIEW);
//                    v.setDataAndType(Uri.parse(outPath), "video/mp4");
//                    startActivity(v);
                }

                @Override
                public void onFailure() {
//                    Toast.makeText(EditActivity.this, "编辑失败", Toast.LENGTH_SHORT).show();

//                    Intent intent = new Intent(getApplicationContext(), PlayVideoActivity.class);
//
//                                                   Bundle bundle = new Bundle();
//                                                   bundle.putString("videoPath", videoUrl);
//                    intent.putExtras(bundle);
//
//                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    getApplicationContext().startActivity(intent);
                }

                @Override
                public void onProgress(float v) {
                    Log.e("jyjyjyjy","过程==="+(int) (v * 100));
                }
            });
        }else{
            Toast.makeText(this, "选择一个视频", Toast.LENGTH_SHORT).show();
        }
    }
}
